<template>
	<view class="details-container">
		<!-- 底部购物车导航栏 -->
		<uni-goods-nav class="goods-nav" :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
			@buttonClick="buttonClick" />
		<image class="img" :src="shopList.listPicUrl" mode=""></image>
		<view class="price">
			{{shopList.retailPrice}}
		</view>
		<!-- 商品信息 -->
		<view class="shop-info">
			<view class="shop-desc">
				{{shopList.name}}
			</view>
			<view class="ratio">
				<view>99.7%</view>
				<view>好评率</view>
			</view>
		</view>
		<view class="self-info">
			<text class="icon-self"></text>
			<text class="icon-shop"></text>
			<text>润本</text>
		</view>
		<view class="recommend">
			推荐理由
		</view>
		<view class="triangle"></view>
		<view class="recommend-content">
			<view class="simple-desc">
				{{shopList.simpleDesc}}
			</view>
		</view>
		<!-- 边界线 -->
		<view class="line">
		</view>
		<!-- 邮费 -->
		<view class="postage">
			<view class="text">
				邮费：单件包邮
			</view>
			<uni-icons type="forward" size="20"></uni-icons>
		</view>
		<view class="cash">
			<view class="text">
				购物返：最高返1积分
			</view>
			<uni-icons type="forward" size="20"></uni-icons>
		</view>
	</view>
</template>

<script>
	import Vue from 'vue'
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				userInfo: {},
				shopList: {},
				options: [{
					icon: 'home',
					text: '首页'
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0,
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ffa200',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ff0000',
						color: '#fff'
					}
				]
			}
		},
		mounted() {
			uni.getStorage({
				key: 'userInfo',
				success: (res) => {
					console.log(res);
					this.userInfo = res.data;
				},
			})
			this.options[1].info = this.totalCount;
		},
		computed: {
			...mapState({
				cartList: state => state.cart.cartList
			}),
			...mapGetters(['totalCount']),
		},
		methods: {
			...mapMutations(['addShopList']),

			onClick(e) {
				// uni.showToast({
				// 	title: `点击${e.content.text}`,
				// 	icon: 'none'
				// })
				console.log(e);

				if (e.content.text == "首页") {
					uni.reLaunch({
						url: '/pages/index/index'
					})
				};
				if (e.content.text == "购物车") {
					uni.reLaunch({
						url: '/pages/cart/cart'
					})
				}
			},
			buttonClick(e) {
				console.log(e)
				if (e.content.text == "加入购物车") {
					if (this.userInfo.nickName) {
						this.addShopList(this.shopList);
						this.options[1].info = this.totalCount;
						uni.showToast({
							title: '已加入购物车！',
							duration: 2000
						});
					} else {
						uni.showModal({
							content: '你还没登录呢，先去登录吧！',
							success: (res) => {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/login/login'
									})
								}
							}
						})
					}

				}

				if (e.content.text == "立即购买") {
					if (this.userInfo.nickName) {
						if (this.cartList.length == 0) {
							this.addShopList(this.shopList);
							this.options[1].info = this.totalCount;
							uni.navigateTo({
								url: '/pages/order/order'
							})
						}else{
							uni.navigateTo({
							url: '/pages/order/order'
						})
						}
						
					} else {
						uni.showModal({
							content: '你还没登录呢，先去登录吧！',
							success: (res) => {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/login/login'
									})
								}
							}
						})
					}
				}
			}
		},
		onLoad(options) {
			this.shopList = JSON.parse(options.shopItem);
			// console.log(this.shopList);
		}
	}
</script>

<style lang="scss">
	.details-container {
		width: 100vw;
		height: 100vh;
		padding-bottom: 100rpx;
		overflow-y: auto;
		box-sizing: border-box;

		.goods-nav {
			position: fixed;
			left: 0;
			bottom: 0;
			width: 100%;
		}

		.img {
			width: 100%;
			height: 60vh;
		}

		.price {
			margin: 20rpx;
			font-size: 64rpx;
			color: red;

			&::before {
				content: '¥';
				display: inline-block;
				font-size: 36rpx;
			}
		}


		.shop-info {
			display: flex;

			.shop-desc {
				flex: 3;
				margin-left: 20rpx;
				font-weight: 600;
				color: #444;
			}

			.ratio {
				flex: 1;
				text-align: center;

				view {
					&:nth-child(1) {
						color: red;
					}

					&:nth-child(2) {
						font-size: 24rpx;
						color: #7f7f7f;
					}
				}
			}
		}

		.self-info {
			display: flex;
			align-items: center;
			margin-left: 20rpx;
			margin-top: 10rpx;
			font-size: 24rpx;
			color: #7f7f7f;

			.icon-self,
			.icon-shop {
				display: inline-block;
				width: 64rpx;
				height: 32rpx;
				background-image: url('../../static/images/self.png');
				background-repeat: no-repeat;
				background-size: cover;

			}

			.icon-shop {
				margin: 0 10rpx;
				width: 28rpx;
				height: 28rpx;
				background-image: url('../../static/images/eco.png');
			}
		}

		.recommend {
			margin: 20rpx 0 20rpx 20rpx;
			font-size: 24rpx;
			color: #7f7f7f;
		}

		.recommend-content {
			padding: 30rpx 20rpx;
			margin: 30rpx 20rpx;
			border: 1rpx solid #ccc;
			font-size: 24rpx;

		}

		.line {
			width: 100%;
			height: 20rpx;
			background: #f4f4f4;
		}

		.postage,
		.cash {
			display: flex;
			align-items: center;
			margin: 0 0 0 20rpx;
			height: 104rpx;
			border-bottom: 1rpx solid #ccc;

			.text {
				flex: 4;
			}

			.icon-right {
				flex: 1;
			}

		}
	}
</style>
